package com.hzqy.service.epg;

import com.hzqy.persistence.dao.epg.AcPhoneBillDao;
import com.hzqy.web.vo.epg.AcPhoneBillVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AcPhoneBillService {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private AcPhoneBillDao acDao;
	public  Map<String,Object> selectAc(AcPhoneBillVo apb, int page, int rows){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("f_apb_cardnumber", apb.getF_apb_cardnumber());
        map.put("f_apb_effectivetime", apb.getF_apb_effectivetime());
        map.put("f_apb_status",apb.getF_apb_status());
        map.put("beginDate",apb.getBeginDate());
        map.put("endDate",apb.getEndDate());
        Integer rowCount = acDao.selectAcCount(map);
        map.put("total", rowCount);
        System.out.println("rowCount:"+rowCount+"page"+page+"rows"+rows);
        if(rowCount>0 && page>0 && rows>0){

            map.put("firstResult", (page-1)*rows);
            map.put("maxResults", rows);
            List<AcPhoneBillVo> list=acDao.selectAcByRequest(map);
            map.put("rows", list);
        }else{
            map.put("rows", new ArrayList<AcPhoneBillVo>());
        }
        return map;
	}
    /**
     * 增加内容---批量入库和单条入库都利用此方法
     * @param phoneBillVo
     * @return
     */
    public Map<String,Object> insertPhoneBillService(AcPhoneBillVo phoneBillVo) {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        int flag = 0;
        if(phoneBillVo==null || phoneBillVo.getF_apb_cardnumber()==null||phoneBillVo.getF_apb_cardkey()==null ||phoneBillVo.getF_apb_effectivetime()==null) {
           //不满足基本数据条件
            flag = -1;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        phoneBillVo.setF_apb_cdate(sdf.format(new Date()));
        phoneBillVo.setF_apb_udate("2099-12-31 23:59:59");
        phoneBillVo.setF_apb_status(1);
        List<AcPhoneBillVo> acPhoneBillVos = acDao.selectByRequest(phoneBillVo);
        if(flag ==0&&acPhoneBillVos.size()==0) {//直接插入数据
            LOG.debug("数据插入成功");
            acDao.insertPhoneBill(phoneBillVo);
            resultMap.put("success", true);
            resultMap.put("result","新增内容成功!");
        }else{//失败
            LOG.debug("数据重复插入失败");
            resultMap.put("success", false);
            resultMap.put("result","新增内容失败!");
        }
        return resultMap;
    }


}
